/**
 * ConfigObject.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package ws4is.engine;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import ws4is.engine.enums.ActionScope;

/**
 * @description Internal object used to list actions services when initializing
 *              dynamic servlet/filter
 */
final public class ConfigObject {

	private String className = null;
	private String cmdGet = null;
	private String cmdPost = null;
	// scope of the action session / request / global
	private String scope = null;
	private Map<String, String> remap = null;

	private transient Constructor constructor = null;
	private transient Map<String, Method> methods = new HashMap<String, Method>();

	public final Constructor getActionConstructor() {
		return constructor;
	}

	public void setDefaultScope(ActionScope as) {
		if (scope == null)
			scope = as.toString();
	}

	public final void setActionClass() {
		try {
			Class clz = Class.forName(this.className);
			constructor = clz.getConstructor();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public final String getRemap(String command) {
		if(remap==null) return command;
		String rm = remap.get(command);
		if (rm == null)
			return command;
		return rm;
	}

	public final Method getMethod(final String method) {
		return methods.get(method);
	}

	public final void setMethod(final String name, final Method method) {
		methods.put(name, method);
	}

	public final String getClassName() {
		return className;
	}

	public final void setClassName(final String className) {
		this.className = className;
	}

	public final String getCmdGet() {
		return cmdGet;
	}

	public final void setCmdGet(final String cmdGet) {
		this.cmdGet = cmdGet;
	}

	public final String getCmdPost() {
		return cmdPost;
	}

	public final void setCmdPost(final String cmdPost) {
		this.cmdPost = cmdPost;
	}

	public ActionScope getScope() {
		return ActionScope.valueOf(scope.toUpperCase());
	}

	public void releaseResources() {
		if (methods!=null) methods.clear();
		if (remap!=null) remap.clear();
		methods = null;
		remap = null;
		className = null;
		cmdGet = null;
		cmdPost = null;
		constructor = null;
	}

}
